//
//  IconButton.swift
//  Asman
//
//  Created by 李旭峰 on 8/12/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit

class IconButton: UIButton {
    enum CXActionItemType {
        case text
        case image
        case horizontalTextImage
        case horizontalImageText
        case verticalTextImage
        case verticalImageText
    }
    
    
    fileprivate var iconLabelTexts:[UIControl.State.RawValue:String] = [:]
    fileprivate var textLabelTexts:[UIControl.State.RawValue:String] = [:]
    fileprivate var iconLabelColors:[UIControl.State.RawValue:UIColor] = [:]
    fileprivate var textLabelColors:[UIControl.State.RawValue:UIColor] = [:]
    
    let viewActions:UIView = {
        let viewActions = UIView()
        viewActions.isUserInteractionEnabled = false
        viewActions.backgroundColor = UIColor.clear
        return viewActions
    }()
    
    let iconLabel: CXLabel = {
        let lb = CXLabel()
        lb.openShadow = false
        //TODO: 用图片替换
//        lb.cxFont = FontAsset.Icon.size(24.0)
        lb.cxFont = FontAsset.PingFangSC_Regular.size(12)
        lb.cxTextColor = UIColor(hexStr: "#FFFFFF", alpha: 0.9)
        lb.textAlignment = NSTextAlignment.center
        return lb
    }()
    
    let textLabel: CXLabel = {
        let lb = CXLabel()
        lb.openShadow = false
        lb.cxFont = FontAsset.PingFangSC_Regular.size(12.0)
        lb.cxTextColor = UIColor(hexStr: "#FFFFFF", alpha: 0.9)
        lb.textAlignment = NSTextAlignment.center
        return lb
    }()
    
    var currentText: String? {
        return self.textLabel.text
    }
    
    var type:CXActionItemType
    init(type:CXActionItemType) {
        self.type = type
        super.init(frame: CGRect.zero)
        self.addObservers()
        self.addSubview(self.viewActions)
        self.viewActions.addSubview(self.iconLabel)
        self.viewActions.addSubview(self.textLabel)
        self.iconLabelColors[UIControl.State.normal.rawValue] = UIColor(hexStr: "#FFFFFF", alpha: 0.9)
        self.textLabelColors[UIControl.State.normal.rawValue] = UIColor(hexStr: "#FFFFFF", alpha: 0.9)
        self.layoutButton()
    }
    public required init?(coder aDecoder: NSCoder) {
        self.type = CXActionItemType.horizontalImageText
        super.init(coder: aDecoder)
        self.addObservers()
        self.addSubview(self.viewActions)
        self.viewActions.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        self.viewActions.addSubview(self.iconLabel)
        self.viewActions.addSubview(self.textLabel)
        self.layoutButton()
    }
    func addObservers(){
        self.addObserver(self, forKeyPath: "enabled", options: [.old,.new], context: nil)
        self.addObserver(self, forKeyPath: "selected", options: [.old,.new], context: nil)
        self.addObserver(self, forKeyPath: "state", options: [.old,.new], context: nil)
    }
    func removeObservers(){
        self.removeObserver(self, forKeyPath: "enabled", context: nil)
        self.removeObserver(self, forKeyPath: "selected", context: nil)
        self.removeObserver(self, forKeyPath: "state", context: nil)
    }
    deinit {
        self.removeObservers()
    }
    func setIcon(_ icon:String?, for state: UIControl.State) {
        self.iconLabelTexts[state.rawValue] = icon
        self.updateLabels()
    }
    func setIconColor(_ color:UIColor?, for state: UIControl.State) {
        self.iconLabelColors[state.rawValue] = color
        self.updateLabels()
    }
    func setText(_ text:String?, for state: UIControl.State) {
        self.textLabelTexts[state.rawValue] = text
        self.updateLabels()
    }
    func setTextColor(_ color:UIColor?, for state: UIControl.State) {
        self.textLabelColors[state.rawValue] = color
        self.updateLabels()
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        self.updateLabels()
    }
    func updateLabels(){
        self.iconLabel.cxText = IconButton.text(state: self.state, dict: self.iconLabelTexts)
        self.iconLabel.cxTextColor = IconButton.color(state: self.state, dict: self.iconLabelColors)
        
        self.textLabel.cxText = IconButton.text(state: self.state, dict: self.textLabelTexts)
        self.textLabel.cxTextColor = IconButton.color(state: self.state, dict: self.textLabelColors)
    }
    fileprivate static func text(state:UIControl.State,dict:[UIControl.State.RawValue:String]) -> String {
        return IconButton.value(state: state, dict: dict, defaultValue: "")
    }
    fileprivate static func color(state:UIControl.State,dict:[UIControl.State.RawValue:UIColor]) -> UIColor {
        return IconButton.value(state: state, dict: dict, defaultValue: UIColor.clear)
    }
    fileprivate static func value<T>(state:UIControl.State,dict:[UIControl.State.RawValue:T],defaultValue:T) -> T {
        var tempValue = defaultValue
        if let normalValue = dict[UIControl.State.normal.rawValue] {
            tempValue = normalValue
        }
        if state &= UIControl.State.disabled {
            return dict[UIControl.State.normal.rawValue] ?? tempValue
        }
        if state &= UIControl.State.selected {
            return dict[UIControl.State.selected.rawValue] ?? tempValue
        }
        if state &= UIControl.State.highlighted {
            return dict[UIControl.State.highlighted.rawValue] ?? tempValue
        }
        return tempValue
    }
}
// MARK: - layout button
extension IconButton {
    func layoutButton(){
        switch self.type{
        case .image:layoutImageStyle()
        case .text:layoutTextStyle()
        case .horizontalImageText:layoutHorizontalImageText()
        case .horizontalTextImage:layoutHorizontalTextImage()
        case .verticalImageText:layoutVerticalImageText()
        case .verticalTextImage:layoutVerticalTextImage()
        }
    }
    fileprivate func layoutImageStyle(){
        self.iconLabel.isHidden = false
        self.textLabel.isHidden = true
        self.iconLabel.textAlignment = .center
        self.textLabel.textAlignment = .center
        
        self.viewActions.snp.remakeConstraints { (make) in
            make.leading.top.trailing.bottom.equalToSuperview()
        }
        self.iconLabel.snp.remakeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.width.height.equalToSuperview()
        }
    }
    
    fileprivate func layoutTextStyle(){
        self.iconLabel.isHidden = true
        self.textLabel.isHidden = false
        self.iconLabel.textAlignment = .center
        self.textLabel.textAlignment = .center
        self.viewActions.snp.remakeConstraints { (make) in
            make.leading.top.trailing.bottom.equalToSuperview()
        }
        self.textLabel.snp.remakeConstraints { (make) in
            make.centerX.width.height.centerY.equalToSuperview()
        }
    }
    
    fileprivate func layoutHorizontalImageText(){
        self.iconLabel.isHidden = false
        self.textLabel.isHidden = false
        self.iconLabel.textAlignment = .right
        self.textLabel.textAlignment = .left
        self.viewActions.snp.remakeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalToSuperview()
        }
        self.iconLabel.snp.remakeConstraints { (make) in
            make.centerY.height.equalToSuperview()
            make.leading.equalToSuperview()
        }
        self.textLabel.snp.remakeConstraints { (make) in
            make.centerY.height.equalToSuperview()
            make.leading.equalTo(self.iconLabel.snp.trailing).offset(3.0)
            make.trailing.equalToSuperview()
        }
    }
    
    fileprivate func layoutHorizontalTextImage(){
        self.iconLabel.isHidden = false
        self.textLabel.isHidden = false
        self.iconLabel.textAlignment = .left
        self.textLabel.textAlignment = .right
        self.viewActions.snp.remakeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalToSuperview()
        }
        self.textLabel.snp.remakeConstraints { (make) in
            make.centerY.height.equalToSuperview()
            make.leading.equalToSuperview()
        }
        self.iconLabel.snp.remakeConstraints { (make) in
            make.centerY.height.equalToSuperview()
            make.leading.equalTo(self.textLabel.snp.trailing).offset(3.0)
            make.trailing.equalToSuperview()
        }
    }
    
    fileprivate func layoutVerticalImageText(){
        self.iconLabel.isHidden = false
        self.textLabel.isHidden = false
        self.iconLabel.textAlignment = .center
        self.textLabel.textAlignment = .center
        self.viewActions.snp.remakeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview()
        }
        self.iconLabel.snp.remakeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.top.equalToSuperview()
        }
        self.textLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(self.iconLabel.snp.bottom).offset(3.0)
            make.bottom.equalToSuperview()
            make.centerX.width.equalToSuperview()
            make.width.equalToSuperview()
        }
    }
    
    fileprivate func layoutVerticalTextImage(){
        self.iconLabel.isHidden = false
        self.textLabel.isHidden = false
        self.iconLabel.textAlignment = .center
        self.textLabel.textAlignment = .center
        self.viewActions.snp.remakeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview()
        }
        self.textLabel.snp.remakeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.width.equalToSuperview()
        }
        self.iconLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(self.textLabel.snp.bottom).offset(3.0)
            make.bottom.equalToSuperview()
            make.centerX.width.equalToSuperview()
        }
    }
}
